import { createStore } from 'vuex'

export default createStore({
  state: {
    name: '洗衣机',
    price: 8600
  },
  getters: {
    getterPrice(state) {
      return state.price + 300
    },
    // 添加更多 getters 用于演示
    formattedPrice(state) {
      return `¥${state.price.toFixed(2)}`
    },
    productName(state) {
      return state.name.toUpperCase()
    }
  },
  mutations: {
    // 修改商品名称
    setName(state, newName) {
      state.name = newName
    },
    // 修改商品价格
    setPrice(state, newPrice) {
      state.price = newPrice
    },
    // 增加价格
    increasePrice(state, amount) {
      state.price += amount
    },
    // 减少价格
    decreasePrice(state, amount) {
      state.price -= amount
    }
  },
  actions: {
    // 异步修改商品名称
    updateNameAsync({ commit }, newName) {
      // 模拟异步操作
      setTimeout(() => {
        commit('setName', newName)
      }, 1000)
    },
    // 异步修改价格
    updatePriceAsync({ commit }, newPrice) {
      // 模拟异步操作
      setTimeout(() => {
        commit('setPrice', newPrice)
      }, 500)
    }
  },
  modules: {
  }
})
